#define _CRT_SECURE_NO_WARNINGS   1

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROWS 10
#define COLS 10
#define MINES 10

void initializeBoard(char board[ROWS][COLS], char display[ROWS][COLS]) {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            board[i][j] = '0';
            display[i][j] = '*';
        }
    }
}

void placeMines(char board[ROWS][COLS]) {
    int minesPlaced = 0;
    while (minesPlaced < MINES) {
        int row = rand() % ROWS;
        int col = rand() % COLS;
        if (board[row][col] != 'M') {
            board[row][col] = 'M';
            minesPlaced++;
        }
    }
}

void calculateNumbers(char board[ROWS][COLS]) {
    int directions[8][2] = {
        {-1, -1}, {-1, 0}, {-1, 1},
        {0, -1},          {0, 1},
        {1, -1}, {1, 0}, {1, 1}
    };

    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (board[i][j] == 'M') continue;
            int count = 0;
            for (int k = 0; k < 8; k++) {
                int ni = i + directions[k][0];
                int nj = j + directions[k][1];
                if (ni >= 0 && ni < ROWS && nj >= 0 && nj < COLS && board[ni][nj] == 'M') {
                    count++;
                }
            }
            board[i][j] = count + '0';
        }
    }
}

void printBoard(char display[ROWS][COLS]) {
    printf("  ");
    for (int j = 0; j < COLS; j++) {
        printf("%d ", j);
    }
    printf("\n");

    for (int i = 0; i < ROWS; i++) {
        printf("%d ", i);
        for (int j = 0; j < COLS; j++) {
            printf("%c ", display[i][j]);
        }
        printf("\n");
    }
}

void revealCell(char board[ROWS][COLS], char display[ROWS][COLS], int row, int col) {
    if (row < 0 || row >= ROWS || col < 0 || col >= COLS || display[row][col] != '*') {
        return;
    }
    display[row][col] = board[row][col];
    if (board[row][col] == '0') {
        int directions[8][2] = {
            {-1, -1}, {-1, 0}, {-1, 1},
            {0, -1},          {0, 1},
            {1, -1}, {1, 0}, {1, 1}
        };
        for (int k = 0; k < 8; k++) {
            int ni = row + directions[k][0];
            int nj = col + directions[k][1];
            revealCell(board, display, ni, nj);
        }
    }
}

int main() {
    char board[ROWS][COLS];
    char display[ROWS][COLS];
    int gameOver = 0;

    srand(time(NULL));
    initializeBoard(board, display);
    placeMines(board);
    calculateNumbers(board);

    while (!gameOver) {
        printBoard(display);
        int row, col;
        printf("Enter row and column: ");
        scanf("%d %d", &row, &col);

        if (row < 0 || row >= ROWS || col < 0 || col >= COLS) {
            printf("Invalid input. Try again.\n");
            continue;
        }

        if (board[row][col] == 'M') {
            printf("Game Over! You hit a mine.\n");
            display[row][col] = 'M';
            gameOver = 1;
        }
        else {
            revealCell(board, display, row, col);
        }
    }

    printBoard(board); // Show the full board after game over
    return 0;
}